package com.easemob.chatui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.easemob.EMCallBack;
import com.easemob.chat.*;
import com.easemob.chatui.Constant;
import com.google.gson.Gson;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.utils.StringUtils;
import com.lyw.GeoLocationClient.bean.BmobUser;
import com.lyw.GeoLocationClient.json.Task;
import timber.log.Timber;

import java.util.ArrayList;

public class NewTaskActivity extends BaseActivity {


    private static final String USER = "user";
    private BmobUser mUser;
    public static final int REQUEST_CODE_TASK = 99;
    private EditText mTitle;
    private EditText mIntro;


    public static void startNewTaskActivityActivityForResult(Activity context, BmobUser user, int code) {
        Intent intent = new Intent();
        intent.putExtra(USER, user);
        intent.setClass(context, NewTaskActivity.class);
        context.startActivityForResult(intent, code);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_task);

        mUser = (BmobUser) getIntent().getSerializableExtra(USER);
        Timber.d(mUser.toString());

        mTitle = (EditText) findViewById(R.id.edit_title);
        mIntro = (EditText) findViewById(R.id.edit_introduction);


    }

    /**
     * @param v
     */
    public void save(View v) {
        String title = mTitle.getText().toString();
        if (StringUtils.isNullOrEmpty(title)) {
            Toast.makeText(this, "please input title", Toast.LENGTH_LONG).show();
        } else {
            Task task = new Task(title, mIntro.getText().toString(),
                    mUser.getUsername(), EMChatManager.getInstance().getCurrentUser());
            Gson gson = new Gson();

            ArrayList<String> tasks = mUser.getTasks();
            if (tasks == null) {
                tasks = new ArrayList<String>();
            }

            String taskSt = gson.toJson(task);
            tasks.add(0,taskSt);
            mUser.setTasks(tasks);

            sendNewTaskMessage(taskSt);

            getIntent().putExtra(USER, mUser);
            setResult(RESULT_OK, getIntent());
            finish();
        }
    }

    private void sendNewTaskMessage(String taskSt) {
        EMMessage message = null;
        TextMessageBody txtBody = null;

        message = EMMessage.createSendMessage(EMMessage.Type.TXT);
        message.setReceipt(mUser.getUsername());
        txtBody = new TextMessageBody(taskSt);
        message.setAttribute(Constant.MESSAGE_NEW_TASK, true);

        message.addBody(txtBody);

        EMChatManager.getInstance().sendMessage(message, new EMCallBack() {
            @Override
            public void onSuccess() {
                Timber.d("success");
            }

            @Override
            public void onError(int i, String s) {
                Timber.d("onError " + s);

            }

            @Override
            public void onProgress(int i, String s) {
                Timber.d("onError " + i + " " + s);

            }
        });
    }
}
